import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2022-05-15
 * Time: 16:30
 */
public class BinaryTree {


     static class TreeNode {
        private char val;
        private TreeNode left;
        private TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        B.left = D;
        B.right = E;
        E.right = H;
        A.right = C;
        C.left = F;
        C.right = G;
        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
         if(root == null) {
             return;
         }
        System.out.print(root.val+" ");
         preOrder(root.left);
         preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }

        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    //后续遍历
    public void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }

       postOrder(root.left);
       postOrder(root.right);
       System .out.print(root.val + " ");
    }

    List<Character> List1 = new ArrayList<>();
    public List<Character> preorderTraversal(TreeNode root) {
        if(root == null) {
            return List1;
        }
        List1.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return List1;
    }

    public List<Character> preorderTraversal2(TreeNode root) {
        List<Character> List2 = new ArrayList<>();
        if(root == null) return List2;

        List2.add(root.val);

        List<Character> cur = preorderTraversal(root.left);
        List2.addAll(cur);

        List<Character> node = preorderTraversal(root.right);
        List2.addAll(node);

        return List2;
    }
}
